package com.zhb.blog.web.admin;

import com.zhb.blog.model.Blog;
import com.zhb.blog.model.User;
import com.zhb.blog.service.BlogService;
import com.zhb.blog.service.TagService;
import com.zhb.blog.service.TypeService;
import com.zhb.blog.vo.BlogQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;
import java.io.InputStream;

@Controller
@RequestMapping("/admin")
public class BlogController {
    private static final String INPUT = "admin/blogs-input";
    private static final String LIST = "admin/blogs";
    private static final String REDIRECT_LIST = "redirect:/admin/blogs";

    @Autowired
    private BlogService blogService;
    @Autowired
    private TypeService typeService;
    @Autowired
    private TagService tagService;
    //博客管理列表
    @GetMapping("/blogs")
    public String blogs(Model model, @PageableDefault(size = 5,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable, BlogQuery blog){
        model.addAttribute("page",blogService.listBlog(pageable,blog));
        model.addAttribute("types",typeService.listType());
        return LIST;
    }
    //进入博客编辑页面
    @GetMapping("/blogs/input")
    public String blogsInput(Model model){
        model.addAttribute("types",typeService.listType());
        model.addAttribute("tags",tagService.listTag());
        model.addAttribute("blog",new Blog());//传给前端，防止报错
        return INPUT;
    }
    //博客修改
    @GetMapping("/blogs/{id}/input")
    public String editInput(@PathVariable Long id, Model model){
        model.addAttribute("types",typeService.listType());
        model.addAttribute("tags",tagService.listTag());
        Blog blog = blogService.getBlog(id);
        blog.init();
        model.addAttribute("blog",blog);
        return INPUT;
    }
    //删除博客
    @GetMapping("/blogs/{id}/delete")
    public String deleteBlog(@PathVariable Long id,RedirectAttributes attributes){
        blogService.deleteBlog(id);
        attributes.addFlashAttribute("msg","操作成功");

        return REDIRECT_LIST;
    }

    //搜索博客方法
    @PostMapping("/blogs/search")
    public String search(Model model, @PageableDefault(size = 5,sort = {"updateTime"},direction = Sort.Direction.DESC) Pageable pageable, BlogQuery blog){
        model.addAttribute("page",blogService.listBlog(pageable,blog));
        return "admin/blogs :: blogList"; //将数据仅返回给前端blogList片段（Thymeleaf+SpringBoot联合实现）
    }
    //添加、修改博客方法
    @PostMapping("/blogs")
    public String post(Blog blog, RedirectAttributes attributes, HttpSession session){
        blog.setUser((User) session.getAttribute("user"));
        blog.setType(typeService.getType(blog.getType().getId()));
        blog.setTags(tagService.listTag(blog.getTagIds()));
        Blog b;
        if(blog.getId() == null){
            //如果获取到的id为空，那么就是新增博客
            b = blogService.saveBlog(blog);
        }else{
            //如果获取到的id不为空，那么就是修改博客
            b = blogService.updateBlog(blog.getId(),blog);
        }

        if(b == null){
            attributes.addFlashAttribute("msg","操作失败");
        }else{
            attributes.addFlashAttribute("msg","操作成功");
        }

        return REDIRECT_LIST;
    }

}
